var phoneUser,nameUser,userId
var childMess={};
$(function () {
    //账户资料
    $("#person").click(function(){
    	var userId=$(this).attr("data-userId")
    	window.location.href='index_edit.html?userId=' + userId +''
    })
    $(".imgCodeDiv").click(function(){
    	window.location.href='extension.html?num=1'
    })
    function all() {
    	//获取个人信息
        slf.ajax({
            url: '/myCenter/getUserInfo',
            success: function (data) {
                person(data);
            }
        });
        //校区设置
        /*slf.ajax({
            url: '/register/getSchoolList',
            success: function (data) {
                school(data);
            }
        });*/
        slf.ajax({
            url: '/myCenter/getUserChildrenCourse',
            type: 'post',
            param: JSON.stringify({ type: "0" }),
            contentType: 'application/json;charset=utf-8',
            success: function (data) {

                childrenCourse(data, "")
            }
        });
        //推广信息
        effectiveNumber();
        /* Promise.all([
             getURL("/myCenter/getUserInfo", "post", null),
             getURL("/register/getSchoolList", "post", null),
             getURL("/myCenter/getUserChildrenCourse", "post", JSON.stringify({type: "0"}))
         ]).then(function (values) {
             //赋值给基本信息
             person(JSON.parse(values[0]))
             //学校信息
             school(JSON.parse(values[1]))
             //孩子课程和账户信息
              console.log(values);
             childrenCourse(JSON.parse(values[2]), "")
             effectiveNumber();
             //getURL("/myCenter/getUserChildrenCourse", "post")
     
         }).catch(function onRejected(error) {
             console.error(error);
         });*/

    }
    /**
     * 基本信息
     * @param data
     */
  function person(data) {
        var info = ""
        childMess=slf.getSequenceChild();
        console.log(data)
        console.log("获取个人信息")
        console.log(childMess)
        console.log("获取孩子信息")
        userId=data.obj.userId;
        phoneUser=data.obj.phone;
        nameUser=data.obj.name;
        if (data.res > 0) {
        	$("#myLfb span").html(data.obj.lfb)
            sessionStorage.setItem("lfb", data.obj.lfb)
            if(data.obj.flag==1){
            	//普通用户
            	$("#vipDiv").removeClass("am-hide")
            }
            var myCode=data.obj.url;
            $("#promotionCode").attr("src", data.obj.qrCode);
            $('#myGrowthValue').html('成长值：' + (data.obj.rechargeTotal == undefined ? 0 : data.obj.rechargeTotal));
        }
    }
    /**
     * 账户信息和孩子课程
     * @param data
     */
    function childrenCourse(courseInfo) {
    	console.log(courseInfo)
    	console.log("账户信息")
        var accountHtml = "";
        var courseHtml = "";
        var children = courseInfo.obj[0];
        sessionStorage.setItem("childrenId", courseInfo.obj[0].childrenId);
        sessionStorage.setItem("schoolValue", courseInfo.obj[0].schoolName);
        //我的推广
        var promotionCode = courseInfo.obj[0].childrenQrCode;

       /* promotion(promotionCode);*/
        // var childrenCourse = children.childrenCourse

        var childrenCourse = children.childrenCoursePage;

        if (courseInfo.res > 0) {
            $("#defaultChild").html(children.name);

            $('#myLfb').find('span').html(sessionStorage.getItem("lfb"));
            //孩子课程
            childrenCourse.forEach(function (obj, index) {
                if (index != courseInfo.obj.length - 1) {
                    courseHtml += "<li class=\"dis-flex\"><div class=\"w40 flex1\">"
                    // courseHtml += "<div class=\"img_div3\"><img src=\"../../img/hdxfd.png\"/></div></div>"
                    courseHtml += "<div class=\" line25 am-margin-left-sm w60\">"
                    courseHtml += "<span class=\"w100b am-block font14 ycsn text-333\">" + obj.name + "(剩余" + obj.activeNumber + "节课)</span>"
                    /* if (obj.state == 0) {
                         courseHtml += "<span class=\"w100b am-block font12 ycsn text-03C481\">未使用</span></div>"
                     } else {
                         courseHtml += "<span class=\"w100b am-block font12 ycsn text-03C481\">已使用</span></div>"
                     }*/
                    courseHtml += "</li>"
                } else {
                    courseHtml += "<li class=\"dis-flex\"><div class=\"w40 flex1\">"
                    //  courseHtml += "<div class=\"img_div3\"><img src=\"../../img/hdxfd.png\"/></div></div>"
                    courseHtml += "<div class=\" line25 am-margin-left-sm w60\">"
                    courseHtml += "<span class=\"w100b am-block font14 ycsn text-333\">" + obj.name + "(剩余" + obj.activeNumber + "节课)</span>"
                    courseHtml += "<span class=\"text-9F9F9F am-align-right am-margin-0\" onclick=\"javascript:window.location.href='../class_time.html'\">查看更多<i class=\"am-icon-angle-double-right am-margin-left-xs\"></i></span></span>"

                    /*  if (obj.state == 0) {
                        courseHtml +="<span class=\"w100b am-block font12 dis-flex\">"
                     //   courseHtml +="<span class=\"text-03C481 flex1 ycsn\">未使用</span>"
                        courseHtml +="<span class=\"text-9F9F9F am-align-right am-margin-0\" onclick=\"javascript:window.location.href='../class_time.html'\">查看更多<i class=\"am-icon-angle-double-right am-margin-left-xs\"></i></span></span>"
                    } else {
                        courseHtml +="<span class=\"w100b am-block font12 dis-flex\">"
                      //  courseHtml +="<span class=\"text-03C481 flex1 ycsn\">已使用</span>"
                        courseHtml +="<span class=\"text-9F9F9F am-align-right am-margin-0\" onclick=\"javascript:window.location.href='../class_time.html'\">查看更多<i class=\"am-icon-angle-double-right am-margin-left-xs\"></i></span></span>"
                     }*/
                    courseHtml += "</li>"
                }



            })


            /*
            courseHtml += "<li class=\"dis-flex posi-rela\"><div class=\"line25 w60\"><ul class=\"am-avg-sm-1 font14 line25 text-333\">"
            courseHtml += "<li>当前拥有币：<span class=\"text-FF0305\">"+sessionStorage.getItem("lfb")+"</span></li>"*/
            //        courseHtml += "<li>当前等级：VIP3</li>"
            //        courseHtml += "<li>成长值：50000</li></ul></div>"
            courseHtml += "<div class=\"w40 flex1\">"
            //  courseHtml += "<button class=\"am-btn am-align-right am-margin-0 radius20 font14 buy_classbtn\" onclick=\"javascript:window.location.href='../recharge.html'\">充值</button></div>"
            //   courseHtml+="	<button class=\"am-btn am-align-right am-margin-0 radius20 font14 buc_classbtn\" onclick=\"javascript:window.location.href='../recharge.html'\">充值</button>"

            //courseHtml += "<a class=\"text-9F9F9F am-align-right am-margin-0 font12 class_more\" href=\"../VIP.html\">查看更多<i class=\"am-icon-angle-double-right am-margin-left-xs\"></i></a></li>"
            $("#childrenCourse").append(courseHtml)
        } else {
            console.log("childrenCourse:" + courseInfo.resMsg)
        }
    }
    /**
     * 校区设置
     */
/*  function school(data) {
        var schoolHtml = ""
        var schoolValue = sessionStorage.getItem("schoolValue")
        data.obj.forEach(function (obj, index) {
            if (obj.value == schoolValue) {
                schoolHtml += "<option value="+obj.value+" selected ='selected '>"+obj.value+"</option>";
            } else {
                schoolHtml += "<option value="+obj.value+">"+obj.value+"</option>";
            }
        })

        $("#school").append(schoolHtml);
    }*/

    /**
     * 修改学校校区
     */
/*$('#school').on('change',function () {
        var childrenId = sessionStorage.getItem("childrenId")

        //学校value
        var schoolValue = $('#school option:selected').val();
        //    getURL("/myCenter/updateChildrenSchool", "post", JSON.stringify({schoolName: schoolValue,childrenId:childrenId})).then(function (value) {
        //    	   sessionStorage.setItem("schoolValue",schoolValue);
        //    })

        slf.ajax({
            url: '/myCenter/updateChildrenSchool',
            type: 'post',
            param: JSON.stringify({ schoolName: schoolValue, childrenId: childrenId }),
            contentType: 'application/json;charset=utf-8',
            success: function (data) {
                sessionStorage.setItem("schoolValue", schoolValue);
            }
        });

    });*/

    function effectiveNumber() {
        slf.ajax({
            url: '/recommendMoney/effectiveNumber',
            success: function (data) {
            	console.log("获取推广返回信息")
            	console.log(data)
                if (data.res > 0) {
                    $('.promotionNum').text(data.obj[0]);
                    $('.promotionMoney').text(data.obj[1]);
                    $(".promotionMoneys").text(data.obj[2])
                }
            }
        });
    }
    $('#cancellation').click(function () {
        slf.ajax({
            url: '/userLogout',
            success: function (data) {
            	console.log(data)
            	console.log("注销")
                if (data.res == 0) {
                    window.location.href = '../login.html';
                }
            }
        });
    });

})
function rechargeBtn(){
	window.location.href='../recharge.html?userId='+userId+'&phone='+phoneUser+'&name='+nameUser+''
}

/**
 * 赋值孩子头像以及课程信息
 * @param data
 */
function top1(data){
    //孩子头像
    $("#headImg").attr('src',data.obj[0].headPortrait)
    $("#childName").html(data.obj[0].name)
}